package com.block.product.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.db.entity.ProductDataUnionCountInfo;
import com.block.db.mapper.ProductDataUnionCountInfoMapper;
import com.block.product.service.IProductDataUnionCountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class ProductDataUnionCountInfoServiceImpl extends ServiceImpl<ProductDataUnionCountInfoMapper, ProductDataUnionCountInfo> implements IProductDataUnionCountInfoService {

    @Autowired
    private ProductDataUnionCountInfoMapper productDataUnionCountInfoMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private String REDIS_LOCK_KEY = "PRODUCT:DATA:COUNT:{}";
    @Override
    public void updateDateUnionCountInfo(ProductDataUnionCountInfo productDataUnionCountInfo) {
        ProductDataUnionCountInfo unionCountInfo = productDataUnionCountInfoMapper.selectByProductIdAndDataInfo(productDataUnionCountInfo.getProductId(),productDataUnionCountInfo.getDateInfo());
        if(ObjectUtil.isEmpty(unionCountInfo)) {
            String redisKey = StrUtil.format(REDIS_LOCK_KEY + productDataUnionCountInfo.getProductId());
            boolean locked = stringRedisTemplate.opsForValue()
                    .setIfAbsent(redisKey, "1", 5, TimeUnit.SECONDS);
            if (locked) {
                unionCountInfo = new ProductDataUnionCountInfo();
                unionCountInfo.setDateInfo(productDataUnionCountInfo.getDateInfo());
                unionCountInfo.setProductId(productDataUnionCountInfo.getProductId());
                unionCountInfo.setProductName(productDataUnionCountInfo.getProductName());
                boolean save = this.save(unionCountInfo);
                if (!save) {
                    System.out.println("保存失败");
                }
                unionCountInfo = productDataUnionCountInfoMapper.selectByProductIdAndDataInfo(productDataUnionCountInfo.getProductId(), productDataUnionCountInfo.getDateInfo());
            } else {
                ThreadUtil.safeSleep(3000);
                unionCountInfo = productDataUnionCountInfoMapper.selectByProductIdAndDataInfo(productDataUnionCountInfo.getProductId(), productDataUnionCountInfo.getDateInfo());
                if (ObjectUtil.isEmpty(unionCountInfo)) {
                    System.out.println("二次查询为空值");
                }
            }
        }

            ProductDataUnionCountInfo updateEntity =   new ProductDataUnionCountInfo();
            updateEntity.setId(unionCountInfo.getId());
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getCheckNumber())){
                updateEntity.setCheckNumber(productDataUnionCountInfo.getCheckNumber() + unionCountInfo.getCheckNumber());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getCheckNumberApp())){
                updateEntity.setCheckNumber(productDataUnionCountInfo.getCheckNumberApp() + unionCountInfo.getCheckNumberApp());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getCheckNumberH5())){
               updateEntity.setCheckNumberH5(productDataUnionCountInfo.getCheckNumberH5() + unionCountInfo.getCheckNumberH5());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionNumberMini())){
                updateEntity.setCheckNumberMini(productDataUnionCountInfo.getCheckNumberMini() +   unionCountInfo.getCheckNumberMini());
            }

            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getCheckSuccessNumber())){
                updateEntity.setCheckSuccessNumber(productDataUnionCountInfo.getCheckSuccessNumber() +  unionCountInfo.getCheckSuccessNumber());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getCheckSuccessNumberApp())){
                updateEntity.setCheckSuccessNumberApp(productDataUnionCountInfo.getCheckSuccessNumberApp() +   unionCountInfo.getCheckSuccessNumberApp());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getCheckSuccessNumberMini())){
                updateEntity.setCheckSuccessNumberMini(productDataUnionCountInfo.getCheckSuccessNumberMini() +   unionCountInfo.getCheckSuccessNumberMini());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getCheckSuccessNumberH5())){
                updateEntity.setCheckSuccessNumberH5(productDataUnionCountInfo.getCheckSuccessNumberH5() +   unionCountInfo.getCheckSuccessNumberH5());
            }

            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionNumber())){
                updateEntity.setUnionNumber(productDataUnionCountInfo.getUnionNumber() +  unionCountInfo.getUnionNumber());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionNumberApp())){
                updateEntity.setUnionNumberApp(productDataUnionCountInfo.getUnionNumberApp() +  unionCountInfo.getUnionNumberApp());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionNumberMini())){
                updateEntity.setUnionNumberMini(productDataUnionCountInfo.getUnionNumberMini()  +   unionCountInfo.getUnionNumberMini());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionNumberH5())){
                updateEntity.setUnionNumberH5(productDataUnionCountInfo.getUnionNumberH5() +   unionCountInfo.getUnionNumberH5());
            }

            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionSuccessNumber())){
                updateEntity.setUnionSuccessNumber(productDataUnionCountInfo.getUnionSuccessNumber() +  unionCountInfo.getUnionSuccessNumber());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionSuccessNumberApp())){
                updateEntity.setUnionNumberApp(productDataUnionCountInfo.getUnionSuccessNumberApp() +  unionCountInfo.getUnionSuccessNumberApp());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionSuccessNumberMini())){
                updateEntity.setUnionNumber(productDataUnionCountInfo.getUnionSuccessNumberMini()  +  unionCountInfo.getUnionSuccessNumberMini());
            }
            if(ObjectUtil.isNotEmpty(productDataUnionCountInfo.getUnionSuccessNumberH5())){
                updateEntity.setUnionSuccessNumberH5(productDataUnionCountInfo.getUnionSuccessNumberH5() +  unionCountInfo.getUnionSuccessNumberH5());
            }

            this.updateById(updateEntity);

    }
}
